<template>
  <div class="article-edit">
    <el-card class="!border-none" shadow="never">
      <el-page-header :content="$route.meta.title" @back="$router.back()" />
    </el-card>
    <el-card class="mt-4 !border-none" shadow="never">
      <el-form
          ref="formRef"
          class="ls-form"
          :model="formData"
          label-width="85px"
          :rules="rules"
      >
        <div class="xl:flex">
          <div>
            <el-form-item label="赛事名称" prop="name">
              <div class="w-80">
                <el-input v-model="formData.name" placeholder="请输入赛事名称" />
              </div>
            </el-form-item>
            <el-form-item label="文章封面" prop="image">
              <div>
                <div>
                  <material-picker v-model="formData.cover" :limit="1" />
                </div>
                <div class="form-tips">建议尺寸：240*180px</div>
              </div>
            </el-form-item>
            <el-form-item label="文章简介" prop="description">
              <div class="w-80">
                <el-input
                    v-model="formData.description"
                    placeholder="请输入文章简介"
                    type="textarea"
                    :autosize="{ minRows: 3, maxRows: 6 }"
                    :maxlength="200"
                    show-word-limit
                    clearable
                />
              </div>
            </el-form-item>
            <el-form-item label="比赛时间" prop="playTime">
              <div class="w-80">
                <el-date-picker
                    style="width: 100%"
                    v-model="formData.playTime"
                    type="daterange"
                    range-separator="到"
                    start-placeholder="比赛开始时间"
                    end-placeholder="比赛结束时间"
                    format="YYYY-MM-DD"
                    value-format="YYYY-MM-DD"
                    clearable
                />
              </div>
            </el-form-item>
            <el-form-item label="报名时间" prop="registrationTime">
              <div class="w-80">
                <el-date-picker
                    style="width: 100%"
                    v-model="formData.registrationTime"
                    type="daterange"
                    range-separator="到"
                    start-placeholder="报名开始时间"
                    end-placeholder="报名结束时间"
                    format="YYYY-MM-DD"
                    value-format="YYYY-MM-DD"
                    clearable
                />
              </div>
            </el-form-item>
            <el-form-item label="比赛方式" prop="playType">
              <div class="w-80">
                <el-select style="width: 100%" v-model="formData.playType" placeholder="请选择比赛方式" multiple clearable>
                  <el-option value="0" label="全类型"></el-option>
                  <el-option value="1" label="男单打"></el-option>
                  <el-option value="2" label="女单打"></el-option>
                  <el-option value="3" label="男双打"></el-option>
                  <el-option value="4" label="女双打"></el-option>
                  <el-option value="5" label="男女混打"></el-option>
                </el-select>
              </div>
            </el-form-item>
            <el-form-item label="比赛赛程" prop="schedule">
              <div class="w-80">
                <el-select style="width: 100%" v-model="formData.schedule" placeholder="请选择赛事赛程" clearable>
                  <el-option value="1" label="初赛"></el-option>
                  <el-option value="2" label="半决赛"></el-option>
                  <el-option value="3" label="总决赛"></el-option>
                </el-select>
              </div>
            </el-form-item>

            <el-form-item label="初始浏览量" prop="viewNum">
              <div class="w-80">
                <el-input-number style="width: 100%" v-model="formData.viewNum" :min="0" />
              </div>
            </el-form-item>
<!--            <el-form-item label="文章状态" required prop="is_show">-->
<!--              <el-radio-group v-model="formData.is_show">-->
<!--                <el-radio :label="1">显示</el-radio>-->
<!--                <el-radio :label="0">隐藏</el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
          </div>
          <div class="xl:ml-20">
            <el-form-item label="比赛内容" prop="content">
              <editor v-model="formData.content" :height="667"/>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </el-card>
    <footer-btns>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </footer-btns>
  </div>
</template>

<script lang="ts" setup name="articleListsEdit">
import type { FormInstance } from 'element-plus'

import { articleAdd, articleCateAll, articleDetail, articleEdit } from '@/api/article'
import { useDictOptions } from '@/hooks/useDictOptions'
import useMultipleTabs from '@/hooks/useMultipleTabs'
import {competitionAdd, competitionDetail, competitionEdit} from "@/api/competition/competition";

const route = useRoute()
const router = useRouter()
const formData = reactive({
  id: '',
  name: '',
  cover: '',
  detailId: '',
  description: '',
  playTime: [],
  playEndTime: '',
  registrationTime: [],
  registrationEndTime: '',
  playType: [],
  schedule: '',
  viewNum: 0,
  content: ''
})

const { removeTab } = useMultipleTabs()
const formRef = shallowRef<FormInstance>()
const rules = reactive({
  title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
  cid: [{ required: true, message: '请选择文章栏目', trigger: 'blur' }]
})

const getDetails = async () => {
  const data = await competitionDetail({
    id: route.query.id
  })
  Object.keys(formData).forEach((key) => {
    //@ts-ignore
    formData[key] = data[key]
    if(key=='playTime'){
      const playTime = [data.playTime,data.playEndTime]
      console.log(playTime)
      formData[key] = playTime
      console.log(data.playTime)
    }
    if(key=='registrationTime') {
      const registrationTime = [data.registrationTime, data.registrationEndTime]
      formData[key] = registrationTime
    }
    if(key=='playType'){
      console.log(formData[key])
      const arr = JSON.parse('['+formData[key]+']')
      let arr2 = []
      for (const arrKey in arr) {
        arr2.push(arr[arrKey]+'')
      }
      console.log(arr2)
      formData[key] = arr2
    }
  })
}

const { optionsData } = useDictOptions<{
  article_cate: any[]
}>({
  article_cate: {
    api: articleCateAll
  }
})
const handleSave = async () => {
  await formRef.value?.validate()
  console.log(formData.playTime)
  if(formData.playTime){
    formData.playEndTime = formData.playTime[1]
    formData.playTime = formData.playTime[0]
  }
  if(formData.registrationTime){
    formData.registrationEndTime = formData.registrationTime[1]
    formData.registrationTime = formData.registrationTime[0]
  }
  if (route.query.id) {
    await competitionEdit(formData)
  } else {
    await competitionAdd(formData)
  }
  removeTab()
  router.back()
}

route.query.id && getDetails()
</script>
